"""用户模型"""
# 字段类型引入 column列
from sqlalchemy import Column, Integer, String, DateTime, func, Text, text
from sqlalchemy.orm import relationship
from soulapi.db import Base, engine


# 基于BaseNoel定义请求体的结构（json对象）
class User(Base):
    __tablename__ = 'user'  # 数据表表名，__tablename__：设置映射到的表名不设置，默认映射到类名post下
    __table_args__ = {'comment': '用户'}
    us_id = Column(Integer, primary_key=True, index=True, autoincrement=True, server_default=text("0"), comment='用户ID')  # index索引设置,autoincremen自增长
    username = Column(String(30), unique=True, nullable=False, server_default=text("''"), comment='用户登录账号')  # 唯一、不为空,comment添加字节
    password = Column(String(128), nullable=False, server_default=text("''"), comment='登陆密码')
    name = Column(String(25), nullable=False, server_default=text("''"), comment='用户姓名')
    sex = Column(Integer, nullable=False, server_default=text("0"), comment="0=女,1=男")
    photo = Column(String(256), nullable=False, server_default=text("''"), comment="用户头像")
    phone = Column(String(30), unique=True, nullable=False, comment="手机号")
    zhuanye = Column(String(50), nullable=True, server_default=text("''"), comment="专业信息")
    ruxue = Column(String(4), nullable=True, server_default=text("'2017'"), comment="入学年份")
    intro = Column(Text, nullable=True, comment="简介")
    # 关系
    # reply = relationship('Reply', back_populates='user')
    # topic = relationship('Topic', back_populates='user')
    # 级联操作
    # orderinfo = relationship('OrderInfo', backref='user', cascade='all')
    # 时间获取
    created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')


# 在数据库中生成表
Base.metadata.create_all(engine)